<template>
	<view class="container">
		<button @click="showDrawer" type="default">{{selectDay}}</button>
		<button @click="download" type="default" style="margin-top: 20px;">下载</button>
		<text>{{filePath}}</text>
		<uni-drawer ref="drawer">
			<scroll-view style="height: 100%;padding: 10rpx;" scroll-y="true">
				<view v-if="day.length==0">
					没有可用日期
				</view>
				<view v-else>
					<button v-for="(item, index) in day" @click="{{closeDrawer(item)}}" type="default" size="mini"
						style="margin-left: 10rpx;">{{item}}</button>
				</view>
			</scroll-view>
		</uni-drawer>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message" :value="popupMessage">
				<uni-popup-message :type="popupMessage.msgType" :message="popupMessage.messageText" :duration="1000">
				</uni-popup-message>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				serverId: '',
				selectDay: '选择日期',
				tagType: ['default', 'default', 'default', 'default', 'default', 'default', 'default'],
				day: [],
				rank: [],
				popupMessage: {
					type: 'center',
					msgType: 'success',
					messageText: '这是一条成功提示',
					value: ''
				},
			}
		},
		onLoad: function(option) {
			this.serverId = option.serverId
			uni.request({
				url: getApp().globalData.url + '/api/stat/getHasDataDay?serverId=' +
					this.serverId,
				header: {
					'content-type': 'application/json',
					'token': getApp().globalData.token
				},
				success: (res) => {
					if (res.data.code == "00000") {
						this.day = res.data.data
					}
				}
			});
		},
		methods: {
			showDrawer() {
				this.$refs.drawer.open();
			},
			closeDrawer(day) {
				this.selectDay = day
				this.$refs.drawer.close();
			},
			download() {
				let that = this
				if (that.selectDay != '选择日期') {
					uni.downloadFile({
						url: getApp().globalData.url + '/api/stat/download?day=' +
							this.selectDay + '&serverId=' + this.serverId,
						success: (res1) => {
							if (res1.statusCode === 200) {
								uni.saveFile({
									tempFilePath: res1.tempFilePath,
									success: function(res2) {
										uni.openDocument({
											fileType:"xlsx",
											filePath: res2.savedFilePath,
											success: function(res3) {
												console.log('打开文档成功')
											},
										})
									}
								});

							}
						}
					});
				} else {
					that.popupMessage.msgType = "info"
					that.popupMessage.messageText = "请先选择时间"
					that.$refs.message.open()
				}

			},
			fmoney(s) {
				var l = (s + "").split("").reverse()
				var t = ""
				for (var i = 0; i < l.length; i++) {
					t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "")
				}
				return t.split("").reverse().join("")
			}
		},
		onShareAppMessage: function(options) {
			var that = this;
			var shareObj = {
				path: '/pages/menu/menu'
			};
			return shareObj;
		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
	}

	uni-tag {
		margin: 10rpx;
	}
</style>
